package org.edu.modules.message.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.message.entity.VeMsgNotice;
import org.edu.modules.message.service.IVeMsgNoticeService;
import org.edu.modules.message.vo.NoticeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
* @Description: 通知消息Api
* @Author: jeecg-boot
* @Date:   2021-05-10
* @Version: V1.0
*/
@Api(tags="通知消息Api")
@RestController
@RequestMapping("/message/veMsgNoticeApi")
@Slf4j
public class VeMsgNoticeApi extends BaseController<VeMsgNotice, IVeMsgNoticeService> {
   @Autowired
   private IVeMsgNoticeService veMsgNoticeService;

    /**
     * 未读消息数量
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "未读消息数量")
    @ApiOperation(value="未读消息数量", notes="未读消息数量")
    @GetMapping(value = "/qryUnreadCount")
    public Result<?> qryUnreadCount(@RequestParam(name="userId",required=true) String userId) {
        Integer count = veMsgNoticeService.qryUnreadCount(userId);
        return Result.OK(count);
    }

    /**
     * 我的通知列表
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "我的通知列表")
    @ApiOperation(value="我的通知列表", notes="我的通知列表")
    @GetMapping(value = "/myNoticeList")
    public Result<?> myNoticeList(@RequestParam(name="userId",required=true) String userId,
                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<VeMsgNotice> queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_to", userId);
        queryWrapper.orderByDesc("create_time");
        Page<VeMsgNotice> page = new Page<VeMsgNotice>(pageNo, pageSize);
        IPage<VeMsgNotice> pageList = veMsgNoticeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    /**
     *  更新为已读状态
     *
     * @param id
     * @return
     */
    @AutoLog(value = "更新为已读状态")
    @ApiOperation(value="更新为已读状态", notes="更新为已读状态")
    @PostMapping(value = "/updateRead")
    public Result<?> updateRead(@RequestParam(name="id",required=true) String id) {
        VeMsgNotice veMsgNotice = veMsgNoticeService.getById(id);
        veMsgNotice.setStatus(1);
        veMsgNoticeService.updateById(veMsgNotice);
        return Result.OK("已更新为已读!");
    }

    /**
     *   发送
     *
     * @param noticeVo
     * @return
     */
    @AutoLog(value = "发送")
    @ApiOperation(value="发送", notes="发送")
    @PostMapping(value = "/send")
    public Result<?> send(@RequestBody NoticeVo noticeVo) {
        VeMsgNotice veMsgNotice = new VeMsgNotice();
        veMsgNotice.setStatus(0);
        veMsgNotice.setAddress(noticeVo.getAddress());
        veMsgNotice.setAppCode(noticeVo.getAppCode());
        veMsgNotice.setContent(noticeVo.getContent());
        veMsgNotice.setSendBy(noticeVo.getSendBy());
        veMsgNotice.setSendTo(noticeVo.getSendTo());
        veMsgNotice.setSendTime(new Date());
        veMsgNotice.setSendType(noticeVo.getSendType());
        veMsgNotice.setTitle(noticeVo.getTitle());
        veMsgNoticeService.save(veMsgNotice);
        return Result.OK("发送成功！");
    }

    /**
     * 我的发送列表
     *
     * @param userId
     * @return
     */
    @AutoLog(value = "我的发送列表")
    @ApiOperation(value="我的发送列表", notes="我的发送列表")
    @GetMapping(value = "/mySendList")
    public Result<?> mySendList(@RequestParam(name="userId",required=true) String userId,
                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<VeMsgNotice> queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_by", userId);
        queryWrapper.orderByDesc("create_time");
        Page<VeMsgNotice> page = new Page<VeMsgNotice>(pageNo, pageSize);
        IPage<VeMsgNotice> pageList = veMsgNoticeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
}
